/*
* Copyright (C) 2024 ~ 2025 Uniontech Technology Co., Ltd.
*
* Author:      zhangbingyi <zhangbingyi@uniontech.com>
*
* Maintainer:  zhangbingyi <zhangbingyi@uniontech.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "operationdelegate.h"
#include "virusfiledetaildefinition.h"

//#include <DGuiApplicationHelper>

#include <QPainter>
#include <QPixmap>
#include <QIcon>
#include <QMouseEvent>
#include <QApplication>
#include <QDebug>
#include <QToolTip>

#define ICON_SIZE 26 //图标大小
#define ICON_SPACE 20 //左右两边的间隔

OperationDelegate::OperationDelegate(QObject *parent)
    : SuggestDelegate(parent)
    , m_isLightTheme(true)
{
    //主题颜色改变
//    m_isLightTheme = (DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::ColorType::LightType);
//    connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, this, [this](DGuiApplicationHelper::ColorType type) {
//        m_isLightTheme = (type == DGuiApplicationHelper::ColorType::LightType);
//    });
}

void OperationDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItem itemOption(option);
    if (index.column() == COLUMN_OPERATION) {
        painter->save();
        QModelIndex tmpIndex = index.model()->index(index.row(), COLUMN_OPERATION);

        //根据禁用状态设置透明度
        bool operaFlag = index.model()->data(tmpIndex, ROLE_OPERATION_FLAG).toBool();
        if (operaFlag) {
            painter->setOpacity(0.5);
        } else {
            painter->setOpacity(0.8);
        }

        QRect rect = itemOption.rect;
        int spacing = (rect.width() - 3 * ICON_SIZE - 2 * ICON_SPACE) / 2;
        int heightSpacing = (rect.height() - ICON_SIZE) / 2;

        //根据主题绘制对应图标
        if (m_isLightTheme) {
            rect.setRect(rect.x() + ICON_SPACE, rect.y() + heightSpacing, ICON_SIZE, ICON_SIZE);
            painter->drawPixmap(rect, QIcon::fromTheme("icon_repair").pixmap(QSize(ICON_SIZE, ICON_SIZE)));
            rect.setRect(rect.x() + ICON_SIZE + spacing, rect.y(), ICON_SIZE, ICON_SIZE);
            painter->drawPixmap(rect, QIcon::fromTheme("icon_isolation").pixmap(QSize(ICON_SIZE, ICON_SIZE)));
            rect.setRect(rect.x() + ICON_SIZE + spacing, rect.y(), ICON_SIZE, ICON_SIZE);
            painter->drawPixmap(rect, QIcon::fromTheme("icon_trust").pixmap(QSize(ICON_SIZE, ICON_SIZE)));
        } else {
            rect.setRect(rect.x() + ICON_SPACE, rect.y() + heightSpacing, ICON_SIZE, ICON_SIZE);
            painter->drawPixmap(rect, QIcon::fromTheme("icon_repair_dark").pixmap(QSize(ICON_SIZE, ICON_SIZE)));
            rect.setRect(rect.x() + ICON_SIZE + spacing, rect.y(), ICON_SIZE, ICON_SIZE);
            painter->drawPixmap(rect, QIcon::fromTheme("icon_isolation_dark").pixmap(QSize(ICON_SIZE, ICON_SIZE)));
            rect.setRect(rect.x() + ICON_SIZE + spacing, rect.y(), ICON_SIZE, ICON_SIZE);
            painter->drawPixmap(rect, QIcon::fromTheme("icon_trust_dark").pixmap(QSize(ICON_SIZE, ICON_SIZE)));
        }
        painter->restore();
    }
    SuggestDelegate::paint(painter, option, index);
}

bool OperationDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
    QStyleOptionViewItem itemOption(option);
    if (index.column() == COLUMN_OPERATION) {
        // 处理鼠标点击事件
        QRect rect = itemOption.rect;
        QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
        m_mousePoint = mouseEvent->pos();
        int spacing = (rect.width() - 3 * ICON_SIZE - 2 * ICON_SPACE) / 2;
        int heightSpacing = (rect.height() - ICON_SIZE) / 2;
        m_repairRect = QRect(rect.x() + ICON_SPACE, rect.y() + heightSpacing, ICON_SIZE, ICON_SIZE);
        m_isolationRect = QRect(rect.x() + ICON_SPACE + ICON_SIZE + spacing, rect.y() + heightSpacing, ICON_SIZE, ICON_SIZE);
        m_trustRect = QRect(rect.x() + ICON_SPACE + 2 * ICON_SIZE + 2 * spacing, rect.y() + heightSpacing, ICON_SIZE, ICON_SIZE);
        QModelIndex tmpIndex = index.model()->index(index.row(), COLUMN_OPERATION);
        QModelIndex editorIndex = index.model()->index(index.row(), COLUMN_SUGGESTION);
        bool operaFlag = index.model()->data(tmpIndex, ROLE_OPERATION_FLAG).toBool();

        //按钮未被禁用并且点击才发送对应信号
        QModelIndex operaIndex = index.model()->index(index.row(), COLUMN_RISKS);
        if (event->type() == QEvent::MouseButtonRelease && !operaFlag) {
            QString filePath = index.model()->data(operaIndex, ROLE_VIRUSFILE_PATH).toString();
            if (m_repairRect.contains(m_mousePoint)) {
                Q_EMIT openPresistenEditor(editorIndex);
                Q_EMIT suggestStatusChanged(index.row(), 1);
                Q_EMIT singleRowOperation(filePath, Itemtype::Repair);
            } else if (m_isolationRect.contains(m_mousePoint)) {
                Q_EMIT openPresistenEditor(editorIndex);
                Q_EMIT suggestStatusChanged(index.row(), 2);
                Q_EMIT singleRowOperation(filePath, Itemtype::isolation);
            } else if (m_trustRect.contains(m_mousePoint)) {
                Q_EMIT openPresistenEditor(editorIndex);
                Q_EMIT suggestStatusChanged(index.row(), 3);
                Q_EMIT singleRowOperation(filePath, Itemtype::Trust);
            }
        }
    }
    return SuggestDelegate::editorEvent(event, model, option, index);
}

QWidget *OperationDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    return SuggestDelegate::createEditor(parent, option, index);
}

bool OperationDelegate::helpEvent(QHelpEvent *event, QAbstractItemView *view, const QStyleOptionViewItem &option, const QModelIndex &index)
{
    if (event->type() == QEvent::ToolTip) {
        if (index.column() == COLUMN_OPERATION) {
            QString tooltip;
            if (m_repairRect.contains(event->pos())) {
                tooltip = QString(tr("Repair"));
            } else if (m_isolationRect.contains(event->pos())) {
                tooltip = QString(tr("Quarantine"));
            } else if (m_trustRect.contains(event->pos())) {
                tooltip = QString(tr("Trust"));
            } else {
                QWidgetList qwl = QApplication::topLevelWidgets();
                for (QWidget *qw : qwl) {
                    if (QString("QTipLabel") == qw->metaObject()->className()) {
                        qw->close();
                    }
                }
            }
            QToolTip::showText(event->globalPos(), tooltip);
            return true;
        }
    }
    return SuggestDelegate::helpEvent(event, view, option, index);
}
